﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BIS.Portal.Core.Base;
using BIS.Portal.Core.Global;
using BIS.Utility.Converter;
using BIS.Portal.Modules.CMS.BO;
using BIS.Portal.Modules.CMS.Entity;

namespace BIS.Portal.Modules.CMS
{
    public partial class CmsListRelatedArticle : ControlBase
    {
        #region get data settings
        public string SLTinHienThi
        {
            get { return GetControlSetting<string>(ModuleSettingNames.CmsListRelatedArticle.txtSLTienHienThi, PortalContext.LanguageAlias); }
        }
        public bool HienThiAnh
        {
            get
            {
                return GetControlSetting<bool>(ModuleSettingNames.CmsListRelatedArticle.ddlAnhMinhHoa, PortalContext.LanguageAlias);
            }

        }
        public bool HienTHiMota
        {
            get
            {
                return GetControlSetting<bool>(ModuleSettingNames.CmsListRelatedArticle.ddlMoTa, PortalContext.LanguageAlias);
            }

        }
        public bool HienTHiLinkCT
        {
            get
            {
                return GetControlSetting<bool>(ModuleSettingNames.CmsListRelatedArticle.ddlLinkCT, PortalContext.LanguageAlias);
            }

        }
        #endregion
        private string GetCateName(int iCateId)
        {
            return ClientCmsArticleCategoryAndCategoryBO.GetCategoryNameById(iCateId).Category_name;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            loadData();
        }
        private void  loadData()
        {
            int _article_id =Functions.GetInt(Request[ModuleQueryStringKey.ArticleId]);
            IList<ClientCmsArticleAndCategoryEntity> list = ClientCmsArticleBO.GetListRelatedArticle(_article_id, PortalContext.LanguageInUseId, Functions.GetInt(SLTinHienThi));
            Tin_Tuc.DataSource = list;
            Tin_Tuc.DataBind();
            Nhan_Tieu_De.Text = GetLanguageValue("LableTitle");
        }
        protected void Tin_Tuc_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ClientCmsArticleAndCategoryEntity objArticle = (ClientCmsArticleAndCategoryEntity) e.Item.DataItem;

                #region Url Chi Tiết

                string urlChiTiet = WebHelpers.GetLink(ModuleQueryStringKey.UrlLinkChiTiet, objArticle.Page_alias,
                                                       new string[]
                                                           {
                                                               ModuleQueryStringKey.CategoryId,
                                                               ModuleQueryStringKey.ArticleId,
                                                               ModuleQueryStringKey.CategoryName,
                                                               ModuleQueryStringKey.ArticleTile
                                                           },
                                                       new string[]
                                                           {
                                                               Convert.ToString(objArticle.Category_id),
                                                               Convert.ToString(objArticle.Article_id),
                                                               Functions.UnicodeToKoDauAndGach(GetCateName(objArticle.Category_id)),
                                                               Functions.UnicodeToKoDauAndGach(objArticle.Article_title)
                                                           }
                    );

                #endregion
                #region Bind dữ liệu Hyperlink Tiêu đề
                HyperLink lnkTieuDe = e.Item.FindControl("Tieu_De") as HyperLink;
                    if (lnkTieuDe != null)
                    {
                        lnkTieuDe.NavigateUrl = urlChiTiet;
                        lnkTieuDe.Text = objArticle.Article_title;
                    }
               
                #endregion
                #region Bind dữ liêu Image
                if (HienThiAnh)
                {
                    Image imgAnh = e.Item.FindControl("Anh_Minh_Hoa") as Image;
                    if (imgAnh != null)
                    {
                        imgAnh.ImageUrl = objArticle.Thumb_image;
                        imgAnh.AlternateText = objArticle.Article_title;
                        imgAnh.Attributes.Add("onerror", "ImageNotFound(this);");
                    }
                }
                #endregion
                #region Bind Dư liệu nội dung
                if (HienTHiMota)
                {
                    Literal ltrNoiDung = e.Item.FindControl("Mo_ta") as Literal;
                    if (ltrNoiDung != null) ltrNoiDung.Text = objArticle.Article_description;
                }
                #endregion
                #region Bind Dữ liệu Link Chi Tiết
                if (HienTHiLinkCT)
                {
                    HyperLink lnkLinkCT = e.Item.FindControl("Link_Xem_CT") as HyperLink;
                    if (lnkLinkCT != null)
                    {
                        lnkLinkCT.Text = GetLanguageValue("ReadMoreButton");
                        lnkLinkCT.NavigateUrl = urlChiTiet;
                    }
                }
                #endregion
            }
        }
    }
}